#!/usr/bin/env python2.7

__all__ = ['reform_aws']

def reform_aws(aws, days, field='temp'):
    """reform_aws reforms hk aws 30 min records into arrays of (ndays, 48).
    aws: hk aws 30 min data
    days: needed days
    field: 'temp', 'rh', 'vis', 'pressure', 'rad', 'precip'
Return:
    a (ndays, 48) array.
    """
    awsdts = aws['datetime']
    t = aws[field]
    daystrs = [dt.strftime('%Y%m%d') for dt in days]
    daysd = dict(zip(daystrs, range(len(daystrs))))
    res = np.zeros((len(days), 48), dtype='f8')
    res[:] = np.nan
    for i in range(len(aws)):
        dt = awsdts[i]
        ymdstr = dt.strftime('%Y%m%d')
        if ymdstr in daysd:
            ii = daysd[ymdstr]
            jj = (dt.hour*60 + dt.minute) / 30
            res[ii, jj] = t[i]
    return res
